//
//  SingView.m
//  Kugou-OC
//
//  Created by BH on 16/1/22.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "SingView.h"
#import "GlobalDefine.h"
#import "ScrollLabel.h"
#import "SingViewCell.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@interface SingView ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *cellImg;
    NSArray *cellTitle;
}

@property(nonatomic,strong)UITableView *tableV;

@end

@implementation SingView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        cellImg = @[@"home_cell_square_image1.jpg",@"home_cell_square_image2.jpg",@"home_cell_square_image3.jpg",@"home_cell_square_image4.jpg",@"home_cell_square_image5.jpg",@"home_cell_square_image6.jpg",@"home_cell_square_image7.jpg"];
        //添加子视图
        [self addAlphaBgViews];
    }
    
    return self;
}

-(void)addAlphaBgViews{
    UIView *bgView1 = [[UIView alloc] init];
    bgView1.layer.masksToBounds = NO;
    bgView1.layer.cornerRadius = 3;
    bgView1.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView2 = [[UIView alloc] init];
    bgView2.layer.masksToBounds = NO;
    bgView2.layer.cornerRadius = 3;
    bgView2.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView3 = [[UIView alloc] init];
    bgView3.layer.masksToBounds = NO;
    bgView3.layer.cornerRadius = 3;
    bgView3.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView4 = [[UIView alloc] init];
    bgView4.layer.masksToBounds = NO;
    bgView4.layer.cornerRadius = 3;
    bgView4.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView5 = [[UIView alloc] init];
    bgView5.layer.masksToBounds = NO;
    bgView5.layer.cornerRadius = 3;
    bgView5.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    [self sd_addSubviews:@[bgView1,bgView2,bgView3,bgView4,bgView5]];
    
    bgView1.sd_layout.leftSpaceToView(self,10).topSpaceToView(self,10).rightSpaceToView(self,10);
    
    bgView2.sd_layout.leftEqualToView(bgView1).topSpaceToView(bgView1,10/2).rightEqualToView(bgView1);
    
    bgView3.sd_layout.leftEqualToView(bgView2).topSpaceToView(bgView2,10/2).widthRatioToView(self,0.63);
    
    bgView4.sd_layout.leftSpaceToView(bgView3,10/2).topEqualToView(bgView3).rightEqualToView(bgView2);
    
    bgView5.sd_layout.leftEqualToView(bgView3).topSpaceToView(bgView3,10/2).rightEqualToView(bgView4);
    
    //添加subviews
    [self addBgView1Subviews:bgView1];
    [self addBgView2Subviews:bgView2];
    [self addBgView3Subviews:bgView3];
    [self addBgView4Subviews:bgView4];
    [self addBgView5Subviews:bgView5];
}

-(void)addBgView1Subviews:(UIView *)view{
    UITextField *searchTF = ALLOC(UITextField);
    searchTF.placeholder = @"今天想唱什么歌";
    searchTF.returnKeyType = UIReturnKeySearch;
    
    UIImageView *rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    rightImg.image = IMAGE(@"listen_search_gray");
    
    searchTF.rightView = rightImg;
    searchTF.rightViewMode = UITextFieldViewModeAlways;
    
    [view addSubview:searchTF];
    
    searchTF.sd_layout.leftEqualToView(view).topEqualToView(view).rightEqualToView(view).heightIs(44);
    [view setupAutoHeightWithBottomView:searchTF bottomMargin:0];
}

-(void)addBgView2Subviews:(UIView *)view{
    NSArray *btnImgs = @[@"sing_friends_normal",@"sing_conversation_normal",@"",@"sing_appoint_normal",@"sing_my_normal"];
    NSArray *selectImg = @[@"sing_friends_selected",@"sing_conversation_selected",@"",@"sing_appoint_selected",@"sing_my_selected"];
    NSArray *btnTitles = @[@"好友圈",@"聊天",@"K歌",@"约歌",@"我的"];
    UIButton *btn = nil;
    UILabel *lab = nil;
    for (int i=0; i<btnImgs.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:IMAGE(btnImgs[i]) forState:UIControlStateNormal];
        [button setBackgroundImage:IMAGE(selectImg[i]) forState:UIControlStateSelected];
        if (i==2) {
            [button setImage:IMAGE(@"sing_K_image") forState:UIControlStateNormal];
            [button setBackgroundImage:IMAGE(@"sing_K_bgimage") forState:UIControlStateNormal];
        }
        
        UILabel *label = [[UILabel alloc] init];
        label.text = btnTitles[i];
        label.font = TITLE_THIRD_FONT;
        label.textAlignment = NSTextAlignmentCenter;
        
        [view sd_addSubviews:@[button,label]];
        
        if (btn || label) {
            button.sd_layout.leftSpaceToView(btn,0).topSpaceToView(view,0).widthRatioToView(view,0.2).heightEqualToWidth();
            label.sd_layout.leftSpaceToView(lab,0).topSpaceToView(button,5).widthRatioToView(view,0.2).autoHeightRatio(0);
            
        }else{

            button.sd_layout.leftEqualToView(view).topEqualToView(view).widthRatioToView(view,0.2).heightEqualToWidth();
            label.sd_layout.leftEqualToView(view).topSpaceToView(button,5).autoHeightRatio(0).widthRatioToView(view,0.2);
        }
        
        btn = button;
        lab = label;
    }
    
    UIImageView *lineHor = ALLOC(UIImageView);
    lineHor.image = IMAGE(@"listen_horizon_long_line");
    [view addSubview:lineHor];
    lineHor.sd_layout.leftSpaceToView(view,10).rightSpaceToView(view,10).heightIs(1).topSpaceToView(view,100);
    
    ScrollLabel *scrollLab = [[ScrollLabel alloc] initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-50, 40)];
    [view addSubview:scrollLab];
    [scrollLab addLabelWithText:@[@"カカカカ",@"サスケ",@"うずまきナルト"] duration:2.0];
    [view setupAutoHeightWithBottomView:scrollLab bottomMargin:0];
    
    UIImageView *arrowImg = [[UIImageView alloc] init];
    arrowImg.image = IMAGE(@"home_cell_arrow_left");
    [view addSubview:arrowImg];
    arrowImg.sd_layout.rightSpaceToView(view,20).centerYEqualToView(scrollLab).widthIs(10).heightEqualToWidth();
    
    [view setupAutoHeightWithBottomView:scrollLab bottomMargin:5];
    
}

-(void)addBgView3Subviews:(UIView *)view{
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"附近在唱";
    label1.font = TITLE_SECOND_FONT;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"礼物榜|离我最近|最新上传";
    label2.font = TITLE_FOUR_FONT;
    label2.textColor = TITLE_SECOND_TEXT_COLOR;
    
    UIImageView *arrowImg = [[UIImageView alloc] init];
    arrowImg.image = IMAGE(@"home_cell_arrow_left");
    
    [view sd_addSubviews:@[label1,label2,arrowImg]];
    
    label1.sd_layout.leftSpaceToView(view,20).topSpaceToView(view,20).widthIs(80).autoHeightRatio(0);
    label2.sd_layout.leftEqualToView(label1).topSpaceToView(label1,20).autoHeightRatio(0).widthIs(200);
    arrowImg.sd_layout.rightSpaceToView(view,20).centerYEqualToView(view).widthIs(10).heightEqualToWidth();
    [view setupAutoHeightWithBottomView:label2 bottomMargin:20];
}

-(void)addBgView4Subviews:(UIView *)view{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:IMAGE(@"sing_city_rank") forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"同城排行";
    label.font = TITLE_THIRD_FONT;
    label.textAlignment = NSTextAlignmentCenter;
    
    [view sd_addSubviews:@[button,label]];
    
    button.sd_layout.topSpaceToView(view,20).leftEqualToView(view).rightEqualToView(view).heightIs(40);
    label.sd_layout.topSpaceToView(button,5).leftEqualToView(button).rightEqualToView(button).autoHeightRatio(0);
    [view setupAutoHeightWithBottomView:label bottomMargin:15];
}

-(void)addBgView5Subviews:(UIView *)view{
    self.tableV = ALLOC(UITableView);
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    self.tableV.backgroundColor = [UIColor clearColor];
    [view addSubview:self.tableV];
    
    self.tableV.sd_layout.leftEqualToView(view).topSpaceToView(view,0).rightEqualToView(view).heightIs(70*cellImg.count+44);
    
    [view setupAutoHeightWithBottomView:self.tableV bottomMargin:0];
    
    [self setupAutoHeightWithBottomView:view bottomMargin:10];
}

#pragma mark - tableview dasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return cellImg.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SingViewCell *cell = [SingViewCell cellWithTableView:tableView];
    cell.imageV.image = IMAGE(cellImg[indexPath.row]);
    cell.titleLab.text = [NSString stringWithFormat:@"%d",(short)indexPath.row];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 44)];
    
    UILabel *sectionTitle = ALLOC(UILabel);
    sectionTitle.frame = CGRectMake(10, 0, SCREEN_WIDTH-20-15, 43);
    sectionTitle.text = @"热门推荐";
    sectionTitle.font = TITLE_SECOND_FONT;
    [sectionV addSubview:sectionTitle];
    
    UIImageView *lineHor = [[UIImageView alloc] initWithFrame:CGRectMake(sectionTitle.origin.x, 43, sectionTitle.width, 1)];
    lineHor.image = IMAGE(@"listen_horizon_long_line");
    [sectionV addSubview:lineHor];
    
    
    return sectionV;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

@end
